package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.ann.Log;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    public PageResult<Clazz> page(ClazzQueryParam clazzQueryParam){
        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());
        List<Clazz> list =clazzMapper.page(clazzQueryParam);
        Page<Clazz> page = (Page<Clazz>) list;
        return new PageResult<>(page.getTotal(),page.getResult());
    }

    /**
     * 根据id删除班级
     * @param id
     */
    @Log
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);
    }

    /**
     * 添加员工信息
     * @param clazz
     */
    @Log
    public void save(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.save(clazz);
    }

    /**
     * 根据id查询班级信息
     * @param id
     * @return
     */
    public Clazz getById(Integer id) {
        Clazz clazz = new Clazz();
        clazz.setId(id);
        List<Clazz> list = clazzMapper.querySelect(clazz);
        return list.get(0);
    }

    @Log
    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }

    /**
     * 查询所有班级信息
     * @return
     */
    public List<Clazz> list() {
        Clazz clazz = new Clazz();
        return clazzMapper.querySelect(clazz);
    }
}
